<?php
class PublicUserAddressForm extends BasePublicUserAddressesForm
{

   public function configure()
   {

		$this->setNameFormat();
		$this->widgetSchema['addresses_different'] = new sfWidgetFormInputCheckbox();
		$this->useFields(array('first_name','last_name', 'address', 'phone', 'city', 'state', 'zip', 'type', 'email','company_name','reg_code','is_company','identity_code','addresses_different' ));

		$this->widgetSchema['type'] = new sfWidgetFormInputHidden();

		$this->validatorSchema['first_name'] =  new sfValidatorString(array('required' => true), array('required' => 'Firstname required'));
		$this->validatorSchema['last_name'] =  new sfValidatorString(array('required' => true), array('required' => 'Lastname required'));
		$this->validatorSchema['is_company'] =  new sfValidatorPass();
		$this->validatorSchema['addresses_different'] =  new sfValidatorPass();
		$this->validatorSchema['company_name'] =  new sfValidatorString(array('required' => false), array('required' => 'Company name required'));
		$this->validatorSchema['identity_code'] =  new sfValidatorNumber(array('required' => true), array('required' => 'Identitycode required','invalid' => 'Identitycode invalid'));
		$this->validatorSchema['reg_code'] =  new sfValidatorNumber(array('required' => false), array('required' => 'Regcode required','invalid' => 'Regcode invalid'));
		$this->validatorSchema['email'] =  new sfValidatorEmail(array('required' => true), array('required' => 'Email required'));
		$this->validatorSchema['phone'] =  new sfValidatorString(array('required' => true),array('required' => 'Phone required') );
		$this->validatorSchema['address'] =  new sfValidatorString(array('required' => true), array('required' => 'Address required'));
		$this->validatorSchema['city'] =  new sfValidatorString(array('required' => true),array('required' => 'City required') );
		
		$this->validatorSchema['state'] =  new sfValidatorString(array('required' => true),array('required' => 'Region required') );
		$this->validatorSchema['zip'] =  new sfValidatorString(array('required' => true),array('required' => 'Postcode required') );

   }
   
   
    public function bind(array $taintedValues = null, array $taintedFiles = null)
    {
	//if is_company = true - reset some validator options
	if(@$taintedValues['is_company']){
	    $this->validatorSchema['first_name']->setOption('required',false);
	    $this->validatorSchema['last_name']->setOption('required',false);
	    $this->validatorSchema['identity_code']->setOption('required',false);
	    
	    $this->validatorSchema['company_name']->setOption('required',true);
	    $this->validatorSchema['reg_code']->setOption('required',true);
	}
	else $this->validatorSchema["phone"]->setOption('required',false);

	return parent::bind($taintedValues,$taintedFiles);
    }
   

   
   public function setNameFormat(){
   		$this->widgetSchema->setNameFormat($this->getObject()->getType() . '_address[%s]');
   }
}